package com.dwjm.exam.stu_reg;

import com.dwjm.exam.pojo.Student;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet("/sreg")
public class StudentRegHandler extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;charset=utf-8");
        resp.setCharacterEncoding("utf-8");
        req.setCharacterEncoding("utf-8");

        PrintWriter out = resp.getWriter();
        
        try {
            // 从请求参数中获取学生信息
            String studentId = req.getParameter("studentId");
            String name = req.getParameter("name");
            String gender = req.getParameter("gender");
            String birthDateStr = req.getParameter("birthDate");
            String idNumber = req.getParameter("idNumber");
            String college = req.getParameter("college");
            String major = req.getParameter("major");
            String className = req.getParameter("className");
            String status = req.getParameter("status");
            String dormId = req.getParameter("dormId");
            String bedNumber = req.getParameter("bedNumber");
            String phone = req.getParameter("phone");
            String email = req.getParameter("email");
            String emergencyContact = req.getParameter("emergencyContact");
            String username = req.getParameter("username");
            String password = req.getParameter("password");
            
            // 验证必填字段
            if (studentId == null || name == null || gender == null || username == null || password == null ||
                studentId.isEmpty() || name.isEmpty() || gender.isEmpty() || username.isEmpty() || password.isEmpty()) {
                out.write("{\"code\":400,\"msg\":\"学号、姓名、性别、用户名和密码为必填项\"}");
                out.flush();
                out.close();
                return;
            }
            
            // 创建Student对象
            Student studentDTO = new Student();
            studentDTO.setStudentId(studentId);
            studentDTO.setName(name);
            studentDTO.setGender(gender);
            
            // 处理日期
            if (birthDateStr != null && !birthDateStr.isEmpty()) {
                try {
                    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                    Date birthDate = format.parse(birthDateStr);
                    java.sql.Date sqlDate = new java.sql.Date(birthDate.getTime());
                    studentDTO.setBirthDate(sqlDate);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
            
            studentDTO.setIdNumber(idNumber);
            studentDTO.setCollege(college);
            studentDTO.setMajor(major);
            studentDTO.setClassName(className);
            studentDTO.setStatus(status);
            studentDTO.setDormId(dormId);
            studentDTO.setBedNumber(bedNumber);
            studentDTO.setPhone(phone);
            studentDTO.setEmail(email);
            studentDTO.setEmergencyContact(emergencyContact);
            studentDTO.setUserName(username);
            studentDTO.setPassword(password);
            
            // 调用DAO保存学生信息
            StudentDao studentDao = new StudentDaoImpl();
            int result = studentDao.addStudents(studentDTO);
            
            if (result > 0) {
                // 注册成功
                out.write("{\"code\":200,\"msg\":\"注册成功\"}");
            } else if (result == -1) {
                // 账号已存在
                out.write("{\"code\":400,\"msg\":\"账号已存在\"}");
            } else {
                // 注册失败
                out.write("{\"code\":500,\"msg\":\"注册失败，请稍后重试\"}");
            }
        } catch (Exception e) {
            e.printStackTrace();
            out.write("{\"code\":500,\"msg\":\"服务器错误: " + e.getMessage() + "\"}");
        } finally {
            out.flush();
            out.close();
        }
    }
}
